package com.genia.toolbox.security.annotation.ws_interceptor;

import org.apache.cxf.interceptor.Fault;
import org.apache.cxf.interceptor.StaxOutInterceptor;
import org.apache.cxf.message.Message;
import org.apache.cxf.phase.AbstractPhaseInterceptor;
import org.apache.cxf.phase.Phase;

import com.genia.toolbox.basics.tools.shared.PropertiesClass;

/**
 * an annotation used to log all XML outflows. You have just to add this line
 * "@OutInterceptors(interceptors = "
 * com.genia.toolbox.projects.sample_ws_trampo.
 * war.ws.interceptor.CatchOutInterceptor")" under the webservice implementation
 */
public class CatchOutInterceptor extends AbstractPhaseInterceptor<Message> {

	private static Boolean active = true;

	/**
	 * Constructor, simply delegates to superclass.
	 *
	 * @param phase
	 */
	public CatchOutInterceptor(String phase) {
		super(phase);
		addBefore(StaxOutInterceptor.class.getName());
	}

	/**
	 * Constructor, simply delegates to superclass.
	 */
	public CatchOutInterceptor() {
		this(Phase.PRE_STREAM);
	}

	/**
	 * delegate the handling to the parent and then silently ignore exception
	 * that must be accepted.
	 *
	 * @param msg
	 *            the message being processed
	 * @throws Fault
	 *             if an error occured
	 */
	public void handleMessage(Message message) throws Fault {

		if (PropertiesClass.getActiveWsInterceptor()) {
			ReadOutInterceptor roi = new ReadOutInterceptor(message);
			roi.logging();
		}
	}

	public Boolean getActive() {
		return active;
	}

	public void setActive(Boolean active) {
		this.active = active;
	}
}